SUBROUTINE GRAD (NPARS,VECPAR,G)
USE SHARE
IMPLICIT NONE
DOUBLE PRECISION, DIMENSION(900,10) :: num
INTEGER :: i,j,NPARS,ja,ip,jp
DOUBLE PRECISION, DIMENSION(900) :: den
DOUBLE PRECISION:: wt(6000),VECPAR(NPARS),G(NPARS)
DOUBLE PRECISION :: Den_grd(900), vect(900,10)
DO i=1, NMOS   !  compute the summation parts of denominator
   den(i)=zero
   DO jp=1,NPARS
      num(i,jp)=zero
   END DO
   DO j=1, ktr(i)
         DO jp=1,NPARS
            num(i,jp)=num(i,jp)+chars(j,i,jp)*rit(j,i)
         END DO
         sc=zero
         DO jp=1,NPARS
            sc=sc+(vecpar(jp)*chars(j,i,jp))
         END DO
         wt(j)= weig(j,i)+ sc/ktr(i)
         den(i)=den(i)+wt(j)*rit(j,i)
   END DO
   DO jp=1,NPARS
      num(i,jp)=num(i,jp)/ktr(i)
   END DO
END DO
DO i=1, NMOS
   Den_grd(i) = (1.0d0+den(i))**(-gamma)
END DO
DO i =1,NMOS
   DO ip=1,NPARS
      vect(i,ip) = -1.0d0*Den_grd(i)*num(i,ip)/NMOS
   END DO
END DO
DO jp=1,NPARS
   g(jp)=zero
END DO
DO i=1,NMOS
   DO jp=1,NPARS
      g(jp)=g(jp)+vect(i,jp)
   END DO
END DO
RETURN; END
